// myself/order/record-active.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';
import notice from '../../utils/notice.js';

const app = getApp();
let pageIndex = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [
            {
              title: '10月28日',
              text: '09:00~12:00'
            },
            {
              title: '10月29日',
              text: '09:00~12:00'
            },
            {
              title: '10月30日',
              text: '09:00~12:00'
            },
            {
              title: '10月31日',
              text: '19:00~22:00'
            },
            {
              title: '10月28日',
              text: '14:00~16:00'
            },
        ],
        statusBarHeight: 0,
        activeIndex: 0,
        details: {},
        list: [
          {
            status: 1,
            avatar: '/assets/avator.png',
            name: '测试用户1',
            createTime: '2024.10.26 17:20'
          }
        ],
        switchChecked: true,
        total: 0,
        signTotal: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.windowInfo = wx.getWindowInfo();
        // this.setData({
        //     statusBarHeight: this.windowInfo.statusBarHeight
        // })

        // this.orderId = options.orderId;
        // this.getOrder(this.orderId);
        this.activeId = options.activeId;
        network.getJson(api.getTimeByActiveId, {
            activeId: this.activeId
        }).then(res => {
            let times = res.data;
            for (let i = 0; i < times.length; i++) {
                times[i].title = formatDate.formatTime(times[i].startTime, "MM月dd日");
                times[i].text = formatDate.formatTime(times[i].startTime, "HH:mm") + "~" + formatDate.formatTime(times[i].endTime, "HH:mm");
                // if (i == 0) {
                //     times[i].selected = true;
                // }
            }

            this.setData({
                tabs: times
            })
            this.timerId = times[0].id;
            this.partRefresh();
        })
       
        notice.addNotice("ActivePartRefresh", this.partRefresh, this);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("ActivePartRefresh", this);
    },


    backTap() {
        wx.navigateBack();
    },

    tabsChange(event) {
        // this.setData({
        //     activeIndex: event.detail.value
        // })
        pageIndex = 1;
        this.timerId = this.data.tabs[event.detail].id;
        this.getActivePartList(this.activeId, this.timerId);
        this.getSignCount(this.activeId, this.timerId);
    },

    partRefresh() {
        this.getActivePartList(this.activeId, this.timerId);
        this.getSignCount(this.activeId, this.timerId);
    },

    // getOrder(id) {
    //     network.getJson(api.getOrder, {
    //         id
    //     }).then(res => {
    //         let details = res.data;
    //         let times = details.bookingTimes;
    //         for (let i = 0; i < times.length; i++) {
    //             res.data.bookingTimes[i].format = formatDate.formatWeek(times[i].startTime, times[i].endTime);
    //             res.data.bookingTimes[i].selected = true;
    //         }
    //         details.bookingTimes = times;

    //         details.images = details.siteDO.images.split(',');
    //         if (details.activeDO) {
    //             details.images = details.activeDO.images.split(',');
    //         }
    //         details.bookingTime = times;
    //         this.setData({
    //             details
    //         })
    //         if (details.activeDO) {
    //             this.setData({
    //                 switchChecked: details.activeDO.onShelves == 1 ? true : false
    //             })
    //         }
    //     })
    // },

    getActivePartList(activeId, timerId) {
        network.getJson(api.activePartPage, {
            activeId,
            timerId: timerId,
            pageNo: pageIndex,
            pageSize: 20
        }).then(res => {
            let list = res.data.list;
            // for (let i = 0; i < gourp.length; i++) {
                // gourp[i].dateTime = formatDate.formatWeek(gourp[i].startTime, gourp[i].endTime);
                for (let j = 0; j < list.length; j++) {
                    list[j].createTime = formatDate.formatTime(list[j].createTime, 'yyyy.MM.dd HH:mm');
                }
            // }
            this.setData({
                list,
                total: res.data.total
            })
        })
    },

    getSignCount(activeId, timerId) {
        network.getJson(api.signCount, {
            activeId,
            timerId
        }).then(res => {
            this.setData({
                signTotal: res.data
            })
        })
    },

    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    navTap(e) {
        const {
            name,
            lng,
            lat
        } = e.currentTarget.dataset;
        wx.openLocation({
            name,
            latitude: lat,
            longitude: lng
        })
    },

    // switchChange(e) {
    //     wx.showModal({
    //         title: e.detail.value ? '确定要上架当前活动' : '确定要下架当前活动',
    //         content: e.detail.value ? '上架后，活动可对外展示和报名' : '下架后，活动不对外展示和报名',
    //         success: res => {
    //             if (res.confirm) {
    //                 network.postJson(api.updateActive, {
    //                     id: this.data.details.activeId,
    //                     onShelves: e.detail.value ? 1 : 2
    //                 }).then(res => {
    //                     notice.postNotice("ActiveRefresh");
    //                     wx.showToast({
    //                         icon: 'none',
    //                         title: '操作成功'
    //                     })
    //                     this.setData({
    //                         [`details.activeDO.onShelves`]: e.detail.value ? 1 : 2
    //                     })
    //                 })
    //             } else if (res.cancel) {}
    //         }
    //     })

    // },

    // cancelTap() {
    //     wx.showModal({
    //         title: '确定要取消该活动',
    //         content: '取消后不再展示活动',
    //         success: res => {
    //             if (res.confirm) {
    //                 network.postJson(api.activeCancel, {
    //                     id: this.activeId
    //                 }).then(res => {
    //                     this.setData({
    //                         [`details.activeDO.status`]: 99
    //                     })
    //                     notice.postNotice("ActiveRefresh");
    //                     notice.postNotice("RefreshOrder");
    //                 })
    //             } else if (res.cancel) {}
    //         }
    //     })
    // },

    // modifyTap() {
    //     if (this.data.details.activeDO.onShelves == 2) {
    //         wx.navigateTo({
    //             url: '/myself/order/edit-active?orderId=' + this.orderId + '&activeId=' + this.activeId
    //         })
    //     } else {
    //         wx.showToast({
    //             icon: 'none',
    //             title: '请先下架'
    //         })
    //     }
    // },

    cancelSignTap(e) {
        const {
            item,
            index
        } = e.currentTarget.dataset;
        network.postJson(api.activePartUpdate, {
            id: item.id,
            activeId: item.activeId,
            userId: item.userId,
            timerId: item.timerId,
            isSign: 2
        }).then(res => {
            this.setData({
                [`list[${index}].isSign`]: 2
            })
        })
    },

    goRefund(e) {
        const item = e.currentTarget.dataset.item;
        console.log(item)
        wx.navigateTo({
            url: '/myself/order/refund?item=' + encodeURIComponent(JSON.stringify(item))
        })
    }
})